package manage.commons.tags;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import manage.commons.Constants;
import manage.modules.rbac.service.RBACService;

/**
 * 权限控制
 * 
 * @author dongji
 * @since 2012-9-20 下午01:23:18
 */
@SuppressWarnings("unchecked")
public class AccessControl {
	private static RBACService rbacService;

	public static boolean accessUri(String uri, HttpServletRequest request) {
		List<Map<String, Object>> rbac = (List<Map<String, Object>>) request
				.getAttribute(Constants.RBAC);
		return rbacService.hasPermissions(uri, rbac);
	}

	public static boolean accessRange(String uri, HttpServletRequest request,
			int rangeValue) {
		return accessRanges(uri, request, String.valueOf(rangeValue));
	}

	public static boolean accessRanges(String uri, HttpServletRequest request,
			String rangeValue) {
		List<Map<String, Object>> rbac = (List<Map<String, Object>>) request
				.getAttribute(Constants.RBAC);
		return rbacService.hasPermissions(uri, rbac, rangeValue);
	}

	public static String getRangeValueByWorker(String uri, Long workerId) {
		return String.valueOf(rbacService.getRangeValue(uri, workerId));
	}

	public static void setRbacService(RBACService rbacService) {
		AccessControl.rbacService = rbacService;
	}
}
